using System;
using VCards.Extensions;
using VCards.Helpers;
using VCards.Models;

namespace VCards.Processors;

public static class PhotoProcessor
{
    public static string Serialize(VCard vcard)
    {
        if(vcard.Photo == null)
        {
            return string.Empty;
        }

        var extension = vcard.Photo.Extension.Coalesce("jpg");
        var mimeType = ImageMimeHelper.GetMimeType(extension);

        return Base64StringProcessor.SerializeBase64String(vcard.Photo.Contents, "PHOTO", mimeType, vcard.Version);
    }

    private static bool IsBase64(string contents)
    {
        try
        {
            byte[] converted = Convert.FromBase64String(contents);
            return true;
        }
        catch
        {
            //swallow
        }

        return false;
    }

    public static Photo GetPhoto(Token token, VCard vcard)
    {
        bool isEmbedded = false;
        string extension = "jpg";

        var data = token.Values[0];

        if (data.StartsWith("data:image", StringComparison.OrdinalIgnoreCase))
        {
            extension = data.Replace(@"data:image/", string.Empty).Coalesce("jpg");

            /*The value is expected to begin with base64,....
            For example:
            PHOTO:
            AQEEBQAwdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05ldHNjYXBlIENvbW11bm
            ljYXRpb25zIENvcnBvcmF0aW9uMRwwGgYDVQQLExNJbmZvcm1hdGlvbiBTeXN0
            <...remainder of base64-encoded data...>
            ****/

            data = token.Values[1].Split(',')[1];
            isEmbedded = true;
        }
        string val;
        if (!isEmbedded && token.AdditionalKeyMembers.TryGetValue("ENCODING", out val) )
            if (    val == "QUOTED-PRINTABLE" || val == "b")
        {
                isEmbedded = true;
        }

        return new Photo(isEmbedded, extension, data);
    }

    public static void Parse(Token token, ref VCard vcard)
    {
        vcard.Photo = GetPhoto(token, vcard);
    }
}